<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : sqlback.manage.php   FILE_PATH : \\system\sqlback.manage.php
 * 数据库备份
 *
 * @copyright workflow_system 
 * @author 
 * @package 
 * @subpackage 
 * @version Fri Dec 07 15:19:39 CST 2007
 */

require_once("../global.inc.php");

class SqlbackManage extends SystemFrameWork{
	/**
	 * 数据库备份对象
	 * 
	 * @var obj
	 */
	var $obj_sql_back;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;

	function main(){
		/**
		 * 创建加价操作对象
		 */
		if (!is_object($this->obj_sql_back)){
			require_once("system_sqlback.class.php");
			$this->obj_sql_back = new SqlBackClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}

		/**
		 * 语言包
		 */
		$this->getlang("system_sqlback");

		switch ($this->_input['action']){
			case "list":
				$this->_list();
				break;
			case "backup":
				$this->_backup();
				break;
			case "optimize_list":
				$this->_optimize_list();
				break;
			case "optimize":
				$this->_optimize();
				break;
			case "info":
				$this->_info();
				break;
			case "restore":
				$this->_restore();
				break;
			case "restore_two":
				$this->_restore_two();
				break;
			case "del":
				$this->_del();
				break;
			case "make_zip":
				$this->make_zip();
				break;
			default:
				$this->_list();
				break;
		}
	}

	/**
	 * 数据表表名列表
	 */
	function _list(){
		/**
		 * 取数据结构
		 */
		$sql_array = $this->obj_sql_back->sqlList();
		/**
		 * 页面输出
		 */
		$this->output('sql_array',$sql_array);
		$this->showpage('sys_sql_back.manage');
	}

	/**
	 * 数据表表名列表
	 */
	function _optimize_list(){
		/**
		 * 取数据结构
		 */
		$sql_array = $this->obj_sql_back->sqlList();
		/**
		 * 页面输出
		 */
		$this->output('sql_array',$sql_array);
		$this->showpage('sys_sql_back.optimize');
	}

	/**
	 * 数据库备份操作
	 */
	function _backup(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["sql_name"],"require"=>"true","message"=>$this->_lang['langSysSQLPleaseSelectNeedBackDataTable']),
		array("input"=>$this->_input["vol_size"],"require"=>"true","validator"=>"Number","message"=>$this->_lang['langSysSQLFileSizeNullOrIsnotNum']),
		);
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath("error","",$error,1);
		}
		/**
		 * 数据库备份
		 */
		$filedir = BasePath."/sqlback";
		//判断是否可写
		if (is_readable($filedir) == false){
			$this->redirectPath("error","",$filedir.$this->_lang['langSysSQLDirHaveNotReadPower'],1);
		}
		if ($this->obj_sql_back->dumpSql($this->_input["sql_name"],$this->_input)) {
			/*记录操作日志*/
			SystemPowerClass::addSysLog($this->_lang['langSysSQLDatabaseManageBackOk']);
			$this->redirectPath("error","",$this->_lang['langSysSQLDatabaseBackOk'],1);
		}else {
			$this->redirectPath("error","",$this->_lang['langCOperatorLost'],1);
		}
	}

	/**
	 * 优化表
	 */
	function _optimize(){
		/**
		 * 取数据结构
		 */
		$sql_array = $this->obj_sql_back->sqlList();
		if (is_array($sql_array)){
			foreach ($sql_array as $k => $v){
				$line .= "`".$v['Name']."`,";
			}
			$line = trim($line,',');
		}
		/**
		 * 优化表
		 */
		if ($this->obj_sql_back->optimizeTable($line)) {
			$this->redirectPath("error","",$this->_lang['langSysSQLOptimizeOk'],1);
		}else {
			$this->redirectPath("error","",$this->_lang['langCOperatorLost'],1);
		}
	}

	/**
	 * 取表结构
	 */
	function _info(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["name"],"require"=>"true","message"=>$this->_lang['langSysSQLTableNameIsNull']),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath("error","",$error,1);
		}
		/**
		 * 取表结构
		 */
		$sql = $this->obj_sql_back->infoTable($this->_input["name"]);
		
		/**
		 * 页面输出
		 */
		$this->output('sql',$sql);
		$this->showpage('sys_sql_back.info');
	}

	/**
	 * 还原表
	 */
	function _restore(){
		/*取间隔符*/
		$b = $this->obj_sql_back->getIntervals();

		if ($handle = @opendir('..'.$b.'sqlback')) {
			$i = 0;
			while (false !== ($file = @readdir($handle))) {
				if ($file != "." && $file != ".." && strstr($file,'.sql')) {
					$file_line = @substr($file,0,-4);
					$vol_array[$i] = @explode("_",$file_line);
					if ($vol_array[$i][count($vol_array[$i])-1] == '1') {
						$pathfile = '..'.$b.'sqlback'.$b.$file;
						//取备份文件注释
//						$remark = $this->obj_sql_back->getFileRemark($pathfile);
						$file_array[$i]['name'] = $file;
						$file_array[$i]['size'] = intval((@filesize($pathfile)/1024)).'K';
						$file_array[$i]['time'] = @date("Y-m-d H:i:s",@filemtime($pathfile));
						$file_array[$i]['file_num'] = count($vol_array[$i])-1;
						$file_array[$i]['zip_name'] = $vol_array[$i][0].'_'.$vol_array[$i][1].'_'.$vol_array[$i][2];
//						$file_array[$i]['remark'] = $remark;
						unset($remark);
						$i++;
					}
				}
			}
			@closedir($handle);
			@rsort($file_array);
		}
		/**
		 * 页面输出
		 */
		$this->output('vol_remark',$remark);
		$this->output('file_array',$file_array);
		$this->showpage('sys_sql_back.restore');
	}

	/**
	 * 还原数据库
	 */
	function _restore_two(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["filename"],"require"=>"true","message"=>$this->_lang['langSysSQLFileNameIsNull']),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath("error","",$error,1);
		}
		$file_line = @substr($this->_input["filename"],0,-4);
		$file_line_array = @explode("_",$file_line);
		/*取SQL文件*/
		$b = $this->obj_sql_back->getIntervals();
		if ($handle = @opendir('..'.$b.'sqlback')) {
			while (false !== ($file = @readdir($handle))) {
				if ($file != "." && $file != "..") {
					$file_line = @substr($file,0,-4);
					$vol_array = @explode("_",$file_line);
					/*判断是否有分卷文件*/
					if ($file_line_array[0].$file_line_array[1].$file_line_array[2] == $vol_array[0].$vol_array[1].$vol_array[2]){
						$restore_file[] = $file;
					}
				}
			}
			@closedir($handle);
		}
		/*还原数据库*/
		$flag = $this->obj_sql_back->do_safe_restore($restore_file);
		if ($flag == true) {
			/*记录操作日志*/
			SystemPowerClass::addSysLog($this->_lang['langSysSQLDatabaseManageRevertOk']);
			$this->redirectPath("error","./sqlback.manage.php?action=list",$this->_lang['langSysSQLDatabaseRevertOk'],1);
		}else {
			$this->redirectPath("error","",$this->_lang['langCOperatorLost'],1);
		}
	}

	/**
	 * 删除文件
	 */
	function _del(){
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["filename"],"require"=>"true","message"=>$this->_lang['langSysSQLFileNameIsNull']),
		);/*数据表表名*/
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath("error","",$error,1);
		}
		$file_line = @substr($this->_input["filename"],0,-4);
		$file_line_array = @explode("_",$file_line);
		/*取SQL文件*/
		$b = $this->obj_sql_back->getIntervals();
		if ($handle = @opendir('..'.$b.'sqlback')) {
			while (false !== ($file = @readdir($handle))) {
				if ($file != "." && $file != "..") {
					$file_line = @substr($file,0,-4);
					$vol_array = @explode("_",$file_line);
					/*判断是否有分卷文件*/
					if ($file_line_array[0].$file_line_array[1].$file_line_array[2] == $vol_array[0].$vol_array[1].$vol_array[2]){
						$restore_file[] = $file;
					}
				}
			}
			@closedir($handle);
		}

		if (is_array($restore_file)){
			foreach ($restore_file as $v){
				@unlink ('..'.$b.'sqlback'.$b.$v);
			}
		}
		//记录操作日志
		SystemPowerClass::addSysLog($this->_lang['langSysSQLLogDel']);
		$this->redirectPath("error","",$this->_lang['langSysSQLDelFileOk'],1);
	}

	function make_zip(){
		$file_prefix = $this->_input['zip_name'];
		include("zip.class.php");
		header("Content-type:application/x-zip");
		header("Content-Disposition:   attachment;   filename=$file_prefix.zip");
		$zipfile   =   new   zipfile();
		/*取SQL文件*/
		$intervals = $this->obj_sql_back->getIntervals();
		$file_path = "..".$intervals."sqlback";/*文件路径*/
		/*取文件*/
		$archive = array();
		if ($handle = @opendir('..'.$intervals.'sqlback')) {
			$i = 0;
			while (false !== ($file = @readdir($handle))) {
				if ($file != "." && $file != ".." && strstr($file,$file_prefix)) {
					$file_to_zip[$file] = $file_path.$intervals.$file;
					$i++;
				}
			}
			@closedir($handle);
		}
		if (is_array($file_to_zip)){
			foreach($file_to_zip   as   $k=>$v){
				$fp =   @fopen($v,"rb");
				$zipfile   ->   addFile(fread($fp,filesize($v)),$k);
				fclose($fp);
			}
		}
		$out   =   $zipfile   ->   file();
		echo   $out;
	}

}

$sqlbac_manage = new SqlbackManage();
$sqlbac_manage->main();
unset($sqlbac_manage);
?>